package com.atguigu.dga.meta.service.impl;

import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga.meta.constant.MetaConst;
import com.atguigu.dga.meta.mapper.TableMetaInfoExtraMapper;
import com.atguigu.dga.meta.service.TableMetaInfoExtraService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表附加信息 服务实现类
 * </p>
 *
 * @author liuxu
 * @since 2023-10-10
 */
@Service
@DS("dga")
public class TableMetaInfoExtraServiceImpl extends ServiceImpl<TableMetaInfoExtraMapper, TableMetaInfoExtra> implements TableMetaInfoExtraService {
    @Override
    public void initTableMetaInfoExtra(List<TableMetaInfo> tableMetaInfoList) {
        //创建存储实体类的集合
        List<TableMetaInfoExtra> tableMetaInfoExtras = new ArrayList<>(tableMetaInfoList.size());
        //循环从tableMetaInfoList获取数据装载到实体类中
        for (TableMetaInfo tableMetaInfo : tableMetaInfoList) {
            //判断是否已经装载
            TableMetaInfoExtra tableMetaInfoExtra = getOne(
                    new QueryWrapper<TableMetaInfoExtra>().
                            eq("table_name", tableMetaInfo.getTableName()).
                            eq("schema_name", tableMetaInfo.getSchemaName())
            );
            if (tableMetaInfoExtra==null){
                tableMetaInfoExtra = new TableMetaInfoExtra();
                tableMetaInfoExtra.setTableName(tableMetaInfo.getTableName());
                tableMetaInfoExtra.setSchemaName(tableMetaInfo.getSchemaName());
                //技术负责人
                tableMetaInfoExtra.setTecOwnerUserName(MetaConst.TEC_OWNER_USER_NAME_UNSET);
                //业务负责人
                tableMetaInfoExtra.setBusiOwnerUserName(MetaConst.BUSI_OWNER_USER_NAME_UNSET);
                //存储周期
                tableMetaInfoExtra.setLifecycleType(MetaConst.LIFECYCLE_TYPE_UNSET);
                //生命周期天数
                tableMetaInfoExtra.setLifecycleDays(-1L) ;
                //安全级别
                tableMetaInfoExtra.setSecurityLevel(MetaConst.SECURITY_LEVEL_UNSET);

                tableMetaInfoExtra.setDwLevel(getTableLevelByName(tableMetaInfo.getTableName()));

                tableMetaInfoExtra.setCreateTime(new Date());

                tableMetaInfoExtras.add(tableMetaInfoExtra);
            }
            //批量装载
            saveOrUpdateBatch(tableMetaInfoExtras);
        }
    }

    private String getTableLevelByName(String tableName) {
        if(tableName.startsWith("ods")){
            return "ODS";
        } else if (tableName.startsWith("dwd")) {
            return "DWD";
        }else if (tableName.startsWith("dim")) {
            return "DIM";
        }else if (tableName.startsWith("dws")) {
            return "DWS";
        }else if (tableName.startsWith("ads")) {
            return "ADS";
        }else if (tableName.startsWith("dm")) {
            return "DM";
        }else  {
            return "OTHER";
        }
    }
}
